package com.voltbarn.vbsensors;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Log;
import android.util.Xml;

public class BaseFeedParser {

	// names of the XML tags
	//static final String RSS = "rss";
	static final String CHANNEL = "current_observation";
	//static final String ITEM = "current_observation";
	
	static final String DATE = "date";
	static final String WUDATE = "observation_time_rfc822";
	static final String ID = "station_id";
	static final String HUMIDITY = "relative_humidity";
	static final String WINDS = "wind_mph";
	static final String WINDD = "wind_degrees";
	static final String PRESSURE = "pressure_in";
	static final String TEMP = "temp_f";
	static final String T0 = "temp0";
	static final String T1 = "temp1";
	static final String T2 = "temp2"; // Currently unused
	static final String T3 = "temp3"; // Currently unused
	static final String LINK = "link";
	static final String TITLE = "temperature_string";
	static final String RAIN = "precip_1hr_in";
	static final String RAIN24 = "precip_today_in";
	
	//private final URL feedUrl;
	private URL feedUrl;
	
	private static final String TAG = "MyActivity";

	protected BaseFeedParser(String feedUrlString){
		try {
			this.feedUrl = new URL(feedUrlString);
		} catch (MalformedURLException e) {
			Log.e(TAG, "URL malformed: " + feedUrlString);
			//throw new RuntimeException(e);
		}
	}

	protected InputStream getInputStream() {
		try {
			return feedUrl.openConnection().getInputStream();
		} catch (IOException e) {
			Log.e(TAG, "IOException" + e);
			return null;
			//throw new RuntimeException(e);
		}
	}
	
	public List<Message> parse() {
		final Message currentMessage = new Message();
		RootElement root = new RootElement(CHANNEL);
		final List<Message> messages = new ArrayList<Message>();
		
		root.setEndElementListener(new EndElementListener(){
			public void end() {
				messages.add(currentMessage.copy());
			}
		});
		root.getChild(ID).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setID(body);
			}
		});
		root.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setLink(body);
			}
		});
		root.getChild(TEMP).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setTemperature(body);
			}
		});
		root.getChild(T0).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setTemp0(body);
			}
		});
		root.getChild(T1).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setTemp1(body);
			}
		});
		root.getChild(HUMIDITY).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setHumidity(body);
			}
		});
		root.getChild(PRESSURE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setPressure(body);
			}
		});		
		root.getChild(WINDS).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setWinds(body);
			}
		});
		root.getChild(DATE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setDates(body);
			}
		});
		root.getChild(WUDATE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setDates(body);
			}
		});
		root.getChild(RAIN).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setRain(body);
			}
		});		
		root.getChild(RAIN24).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentMessage.setRain24(body);
			}
		});
		
	
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			Log.e(TAG, "Parse failed" );
			//throw new RuntimeException(e);
		}
		return messages;
	}
}